#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "Service.h"
#include "Private.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/kd.h>
#include <unistd.h>
#include <fcntl.h>

/// <summary>
/// TODO
/// </summary>
ServicePtr ServiceCreate(void)
{
	return NULL;
}

/// <summary>
/// TODO
/// </summary>
int ServiceExists(const char *filename)
{
	return 0;
}

/// <summary>
/// TODO
/// </summary>
int ServiceStart(const char *filename)
{
	pid_t pid = fork();
	if (pid < 0)
		return -1;
	if (pid != 0) {
		printf("PID %d\n", pid);
		//waitpid(pid, NULL, 0);
		return -1;
	}

	printf("[Starting] %06d, %s\n", getpid(), filename);

	setsid();
	close(0);
	close(1);
	close(2);

	int c = -1;
	if ((c = OpenConsole(O_RDWR|O_NOCTTY|O_NONBLOCK)) >= 0) {
		(void)ioctl(c, TIOCSCTTY, 1);

		/* Copy to standard output and standard error */
		while (dup (c) < 2)
			;
	}

	execve(filename, NULL, NULL);
	printf("TEST\n");
	return 0;
}
